﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Module ocr_checksum_gen


    Function getNeededIntValueFromChar([Char] As Char) As Long
        Return getAsciiIntFromCharacter([Char]) - 32
    End Function

    Function getNeededCharFromIntValue(intValue As Long) As Char
        Return getAsciiCharFromInt(intValue + 32)
    End Function

    Function getAsciiCharFromInt(intValue As Long) As Char
        Dim _retVal As Char = "~"c

        '      If (intValue) Is Nothing Then
        ' Return _retVal
        ' End If

        Select Case intValue
            Case 45
                _retVal = "-"c
                Exit Select
            Case 46
                _retVal = "."c
                Exit Select

            Case 48
                _retVal = "0"c
                Exit Select
            Case 49
                _retVal = "1"c
                Exit Select
            Case 50
                _retVal = "2"c
                Exit Select
            Case 51
                _retVal = "3"c
                Exit Select
            Case 52
                _retVal = "4"c
                Exit Select
            Case 53
                _retVal = "5"c
                Exit Select
            Case 54
                _retVal = "6"c
                Exit Select
            Case 55
                _retVal = "7"c
                Exit Select
            Case 56
                _retVal = "8"c
                Exit Select
            Case 57
                _retVal = "9"c
                Exit Select
            Case 65
                _retVal = "A"c
                Exit Select
            Case 66
                _retVal = "B"c
                Exit Select
            Case 67
                _retVal = "C"c
                Exit Select
            Case 68
                _retVal = "D"c
                Exit Select
            Case 69
                _retVal = "E"c
                Exit Select
            Case 70
                _retVal = "F"c
                Exit Select
            Case 71
                _retVal = "G"c
                Exit Select
            Case 72
                _retVal = "H"c
                Exit Select
            Case 73
                _retVal = "I"c
                Exit Select
            Case 74
                _retVal = "J"c
                Exit Select
            Case 75
                _retVal = "K"c
                Exit Select
            Case 76
                _retVal = "L"c
                Exit Select
            Case 77
                _retVal = "M"c
                Exit Select
            Case 78
                _retVal = "N"c
                Exit Select
            Case 79
                _retVal = "O"c
                Exit Select
            Case 80
                _retVal = "P"c
                Exit Select
            Case 81
                _retVal = "Q"c
                Exit Select
            Case 82
                _retVal = "R"c
                Exit Select
            Case 83
                _retVal = "S"c
                Exit Select
            Case 84
                _retVal = "T"c
                Exit Select
            Case 85
                _retVal = "U"c
                Exit Select
            Case 86
                _retVal = "V"c
                Exit Select
            Case 87
                _retVal = "W"c
                Exit Select
            Case 88
                _retVal = "X"c
                Exit Select
            Case 89
                _retVal = "Y"c
                Exit Select
            Case 90
                _retVal = "Z"c
                Exit Select
            Case Else
                Throw New Exception("could not find character for Ascii int value: " + intValue.ToString())
                Exit Select
        End Select

        Return _retVal
    End Function

    'ascii value  for each character
    Function getAsciiIntFromCharacter([Char] As Char) As Long

        Dim _retVal As Integer = -1
        Select Case [Char]
            Case "-"c
                _retVal = 45
                Exit Select
            Case "."c
                _retVal = 46
                Exit Select

            Case "0"c
                _retVal = 48
                Exit Select
            Case "1"c
                _retVal = 49
                Exit Select
            Case "2"c
                _retVal = 50
                Exit Select
            Case "3"c
                _retVal = 51
                Exit Select
            Case "4"c
                _retVal = 52
                Exit Select
            Case "5"c
                _retVal = 53
                Exit Select
            Case "6"c
                _retVal = 54
                Exit Select
            Case "7"c
                _retVal = 55
                Exit Select
            Case "8"c
                _retVal = 56
                Exit Select
            Case "9"c
                _retVal = 57
                Exit Select
            Case "A"c
                _retVal = 65
                Exit Select
            Case "B"c
                _retVal = 66
                Exit Select
            Case "C"c
                _retVal = 67
                Exit Select
            Case "D"c
                _retVal = 68
                Exit Select
            Case "E"c
                _retVal = 69
                Exit Select
            Case "F"c
                _retVal = 70
                Exit Select
            Case "G"c
                _retVal = 71
                Exit Select
            Case "H"c
                _retVal = 72
                Exit Select
            Case "I"c
                _retVal = 73
                Exit Select
            Case "J"c
                _retVal = 74
                Exit Select
            Case "K"c
                _retVal = 75
                Exit Select
            Case "L"c
                _retVal = 76
                Exit Select
            Case "M"c
                _retVal = 77
                Exit Select
            Case "N"c
                _retVal = 78
                Exit Select
            Case "O"c
                _retVal = 79
                Exit Select
            Case "P"c
                _retVal = 80
                Exit Select
            Case "Q"c
                _retVal = 81
                Exit Select
            Case "R"c
                _retVal = 82
                Exit Select
            Case "S"c
                _retVal = 83
                Exit Select
            Case "T"c
                _retVal = 84
                Exit Select
            Case "U"c
                _retVal = 85
                Exit Select
            Case "V"c
                _retVal = 86
                Exit Select
            Case "W"c
                _retVal = 87
                Exit Select
            Case "X"c
                _retVal = 88
                Exit Select
            Case "Y"c
                _retVal = 89
                Exit Select
            Case "Z"c
                _retVal = 90
                Exit Select
            Case Else

                Throw New Exception("could not find character")
                Exit Select


        End Select
        Return _retVal

    End Function

    Function GenerateSEMIOcrString(OCRString_fragment As String) As String
        Dim _ocrCheckSum As String = GenerateSEMIOCRCheckSumm(OCRString_fragment)
        Return OCRString_fragment + _ocrCheckSum
    End Function

    Function GenerateSEMIOCRCheckSumm(OCRString_fragment As String) As String
        OCRString_fragment += "A0"

        Dim checkArray As IList(Of Long) = New List(Of Long)()



        Try

            ' 2. Wandeln von ASCII- Zeichen numerische Werte 			

            For i As Integer = 0 To OCRString_fragment.Length - 1
                checkArray.Add(0)

                checkArray(i) = getNeededIntValueFromChar(OCRString_fragment(i))
            Next

            Dim summe As Double = 0
            For _i As Integer = 0 To OCRString_fragment.Length - 1
                ' System.Math.Pow(8.0, 11 - _i);
                summe += checkArray(_i) * System.Math.Pow(8.0, OCRString_fragment.Length - _i - 1)
            Next


            If summe Mod 59 = 0 Then
                Return OCRString_fragment.Substring(OCRString_fragment.Length - 2)
            End If

            If summe Mod 59 <> 0 Then

                Dim _convToBinary As Long = 59 - CLng(summe Mod 59)
                Dim _lowerNumber As Long = _convToBinary Mod 8
                ' 7 = bx000111
                Dim _higherNumber As Long = _convToBinary >> 3


                _lowerNumber += getNeededIntValueFromChar("0"c)
                _higherNumber += getNeededIntValueFromChar("A"c)

                Dim _lowerChar As Char = getNeededCharFromIntValue(_lowerNumber)
                Dim _higherChar As Char = getNeededCharFromIntValue(_higherNumber)

                Return _higherChar.ToString() + _lowerChar.ToString()

            End If
        Catch _e As Exception
            'showError("==> Fehler beim Bestimmen der Checksumme");
            MessageBox.Show("Didn't work.  Make sure you send only the portion of the string which needs to get a checksum added to it.  So, don't send the checksum along with the rest of the string", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Return ""
        End Try


        MessageBox.Show("Didn't work.  Make sure you send only the portion of the string which needs to get a checksum added to it.  So, don't send the checksum along with the rest of the string", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)


        Return ""
    End Function



    Function IsOCRSemiConformant(OCRString As String) As Boolean


        Dim checkArray As IList(Of Long) = New List(Of Long)()


        Try

            ' 2. Wandeln von ASCII- Zeichen numerische Werte 			

            For i As Integer = 0 To OCRString.Length - 1
                checkArray.Add(0)

                checkArray(i) = getNeededIntValueFromChar(OCRString(i))
            Next

            Dim summe As Double = 0
            For _i As Integer = 0 To OCRString.Length - 1

                summe += checkArray(_i) * System.Math.Pow(8.0, 11 - _i)
            Next


            If summe Mod 59 = 0 Then
                Return True
            Else
                Return False
            End If


            Return False
        Catch _e As Exception
            Return False
        End Try


    End Function

    Sub btnCheckOCR_Click(sender As Object, e As EventArgs)
        If IsOCRSemiConformant(Form1.tbOCRStringFragment.Text) Then
            MessageBox.Show("OCR String IS Semi Conformant", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show("OCR String IS NOT Semi Conformant", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub







    'This program is conformant with SEMI Document M12-1103 
    '(Specification for serial alphanumeric marking of the front surface of wafers)






End Module
